package com.guguskins.back.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.guguskins.back.common.annotation.Authorized;
import com.guguskins.back.entity.GuHenHouseTask;
import com.guguskins.back.entity.bo.HenHouseRoteBo;
import com.guguskins.back.entity.vo.HenHousePrizeVo;
import com.guguskins.back.entity.vo.HenHouseTaskVo;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.service.GuHenHouseTaskService;
import com.guguskins.back.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/henHouseTask")
public class GuHenHouseTaskController {

    @Autowired
    private GuHenHouseTaskService henHouseTaskService;

    @GetMapping("/detail/{pnId}")
    @Authorized
    public ResultVo getDetail(@RequestHeader(value = "token", required = false, defaultValue = "") String token,
                              @PathVariable("pnId") Long pnId) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        HenHouseTaskVo henHouseTaskVo = henHouseTaskService.getDetail(userId, pnId);
        return ResultVo.success(henHouseTaskVo);
    }

    @PostMapping("/rote")
    public ResultVo getRote(@RequestHeader(value = "token", required = false, defaultValue = "") String token,
                            @RequestBody HenHouseRoteBo henHouseRoteBo) {
        Long userId = null;
        if (!ObjectUtils.isEmpty(token)) {
            userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        }
        HenHouseTaskVo henHouseTaskVo = henHouseTaskService.getRote(userId, henHouseRoteBo);
        return ResultVo.success(henHouseTaskVo);
    }

    @PostMapping("/submit")
    @Authorized
    public ResultVo submit(@RequestHeader("token") String token,
                           @RequestBody HenHouseRoteBo henHouseRoteBo) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        if (henHouseTaskService.submit(userId, henHouseRoteBo)) {
            return ResultVo.success("提交成功");
        }
        return ResultVo.fail("提交失败");
    }

    /**
     * 使用道具卡
     */
    @PostMapping("/speedUp")
    @Authorized
    public ResultVo speedUp(@RequestHeader("token") String token,
                            @RequestBody GuHenHouseTask henHouseTask) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        if (henHouseTaskService.speedUp(userId, henHouseTask)) {
            return ResultVo.success("使用成功");
        }
        return ResultVo.fail("使用失败");
    }

    /**
     * 取消孵化
     */
    @PostMapping("/cancel")
    @Authorized
    public ResultVo cancel(@RequestHeader("token") String token,
                           @RequestBody GuHenHouseTask henHouseTask) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        if (henHouseTaskService.cancel(userId, henHouseTask)) {
            return ResultVo.success("取消成功");
        }
        return ResultVo.fail("取消失败");
    }

    @PostMapping("/receive")
    @Authorized
    public ResultVo receive(@RequestHeader("token") String token,
                            @RequestBody GuHenHouseTask henHouseTask) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        HenHousePrizeVo henHousePrizeVo = henHouseTaskService.receive(userId, henHouseTask);
        return ResultVo.success(henHousePrizeVo);
    }
}
